const jwt = require('jsonwebtoken')
const white = ['/api/login']
module.exports = (options, app) => {
  return async (ctx, next) => {
    if (white.includes(ctx.path)) {
      await next()
    } else {
      const { authorization } = ctx.request.headers;
      // console.log(authorization, 'authorization')
      try {
        const data = jwt.verify(authorization, app.config.keys)
        ctx.state.user = data; //  redis 
        await next()
      } catch (e) {
        ctx.throw = {
          code: -1,
          msg: e.name
        }
      }
    }
  }
}